package samples.groenefee;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;

class PrisPan extends Panel implements ActionListener, ItemListener {
    Pris00 ap;

    TextField tfb, tfp, tfd;
    Button bR, bB;
    Checkbox cb;
    Label lb;

    public PrisPan(Pris00 ap) {
        this.ap = ap;
        Color bg = new Color(255, 255, 192);
        setBackground(bg);
        Label lbb = new Label("b =", Label.RIGHT);
        Label lbp = new Label("p =", Label.RIGHT);
        Label lbd = new Label("delay:", Label.RIGHT);
        tfb = new TextField(5);
        tfp = new TextField(5);
        tfd = new TextField(5);
        tfb.setText(String.valueOf(ap.b));
        tfp.setText(String.valueOf(ap.p));
        tfd.setText(String.valueOf(ap.delay));
        lb = new Label("   ");
        bR = new Button("Run");
        bR.addActionListener(this);
        bB = new Button("Break");
        bB.addActionListener(this);
        bB.setEnabled(false);
        cb = new Checkbox("pause");
        cb.addItemListener(this);
        cb.setEnabled(false);
        add(lbb);
        add(tfb);
        add(lbp);
        add(tfp);
        add(lbd);
        add(tfd);
        add(lb);
        add(bR);
        add(bB);
        add(cb);
    }

    public void actionPerformed(ActionEvent evt) {
        String tb = tfb.getText();
        String tp = tfp.getText();
        String td = tfd.getText();
        ap.b = Double.parseDouble(tb);
        ap.p = Double.parseDouble(tp);
        ap.delay = Integer.parseInt(td);
        if (evt.getSource() == bR) {
            if (ap.cv.thr == null) {
                ap.cv.start();
                tfb.setEnabled(false);
                tfp.setEnabled(false);
                tfd.setEnabled(false);
                bR.setEnabled(false);
                bB.setEnabled(true);
                cb.setState(false);
                ap.paused = false;
                cb.setEnabled(true);
            }
        }
        if (evt.getSource() == bB) {
            if (ap.cv.thr != null) {
                ap.cv.stop();
                ap.cv.repaint();
                tfb.setEnabled(true);
                tfp.setEnabled(true);
                tfd.setEnabled(true);
                bR.setEnabled(true);
                bB.setEnabled(false);
                cb.setState(false); // -> NA
                ap.paused = false;
                cb.setEnabled(false);
            }
        }
    }

    public void itemStateChanged(ItemEvent evt) {
        ap.paused = cb.getState();
    }
}
